package jhh.servlet;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
@WebServlet("/LineChart")
public class LineChart extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	     doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    //创建主题样式  
	       StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
	       //设置标题字体  
	       standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
	       //设置图例的字体  
	       standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
	       //设置轴向的字体  
	       standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
	       //应用主题样式  
	       ChartFactory.setChartTheme(standardChartTheme); 
		response.setContentType("image/png");
		CategoryDataset dataset = createDataset(request, response);
		String str = "123";
		byte[] bs = str.getBytes();
		String strs = new String("响应时间与线程组个数的关系".getBytes(),"utf-8");
		JFreeChart jFreeChart =ChartFactory.createLineChart("响应时间与线程组个数的关系", null, "平均响应时间", dataset,PlotOrientation.VERTICAL,false,true,false);
		//设置图表的子标题
		jFreeChart.addSubtitle(new TextTitle("按线程组个数"));
		//创建一个标题
		TextTitle textTitle=new TextTitle("日期："+new Date());
		//设置标题字体
		textTitle.setFont(new Font("黑体",0,10));
		//设置标题向下对齐
		textTitle.setPosition(RectangleEdge.BOTTOM);
		//设置标题向右对齐
		textTitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		//添加图表的子标题
		jFreeChart.addSubtitle(textTitle);
		//设置图表的背景色为白色
		jFreeChart.setBackgroundPaint(Color.white);
		//获取图表区域对象
		CategoryPlot categoryPlot=(CategoryPlot) jFreeChart.getPlot();
		categoryPlot.setBackgroundPaint(Color.lightGray);
		categoryPlot.setRangeGridlinesVisible(false);
		//获取显示线条对象
		LineAndShapeRenderer lineAndShapeRenderer=(LineAndShapeRenderer) categoryPlot.getRenderer();
		lineAndShapeRenderer.setBaseShapesVisible(true);
		lineAndShapeRenderer.setDrawOutlines(true);
		lineAndShapeRenderer.setUseFillPaint(true);
		lineAndShapeRenderer.setBaseFillPaint(Color.white);
		//设置折线加粗
		lineAndShapeRenderer.setSeriesStroke(0, new BasicStroke());
		lineAndShapeRenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
		//设置折线拐点
		lineAndShapeRenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
		//将图表以数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, 1500, 600);
		
	}
	 private static CategoryDataset createDataset(HttpServletRequest request, HttpServletResponse response){
		 DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		 HttpSession session = request.getSession();	
		 int[]sum=(int[]) session.getAttribute("sum");
		 int[]count=(int[]) session.getAttribute("count");
		 for(int i=0;i<10;i++){
			 int ave=sum[i]/count[i];
			 dataset.addValue(ave, "响应时间", count[i]+"个线程组");
		 }
		 return dataset;
	 }

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
